package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysRuleDao;
import cn.iot.sys.entity.SysRule;
import cn.iot.sys.service.SysFaceRecordService;
import cn.iot.sys.service.SysRuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysRuleServiceImpl extends ServiceImpl<SysRuleDao, SysRule> implements SysRuleService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    SysFaceRecordService sysFaceRecordService;
    @Override
    public ResultMsg addSysRule(SysRule sysRule) throws UnsupportedEncodingException {
        logger.info("SysRuleServiceImpl addSysRule sysRule{}",sysRule);
        sysRule.setCreateDate(new Date());
        int count = this.baseMapper.insert(sysRule);
        if (count>0){
            sysFaceRecordService.setAccessTimeSchedule(sysRule);
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }

    @Override
    public ResultMsg getSysRuleList(Map<String, Object> map) {
        logger.info("SysRuleServiceImpl getSysRuleList map{}",map);
        int pageNum = Integer.parseInt((String)map.get("pageNum"));
        int pageSize = Integer.parseInt((String)map.get("pageSize"));
        String deviceCode = (String)map.get("deviceCode");
        String name = (String)map.get("name");
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        QueryWrapper<SysRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDel",0);
        if (StrUtil.isNotEmpty(deviceCode)){
            queryWrapper.eq("deviceCode",deviceCode);
        }
        if (StrUtil.isNotEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if (StrUtil.isNotEmpty(startDate) && StrUtil.isNotEmpty(endDate)){
            queryWrapper.between("create_Date", startDate, endDate);
        }
        queryWrapper.orderByDesc("create_Date");
        IPage<SysRule> page = new Page<>(pageNum,pageSize);
        IPage<SysRule> page1 = this.baseMapper.selectPage(page, queryWrapper);
        int count = this.baseMapper.selectCount(queryWrapper);
        int pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        List<SysRule> sysRuleList = page1.getRecords();
        logger.debug("SysRuleServiceImpl getSysRuleList sysRuleList{}",sysRuleList);
        map.put("sysRuleList",sysRuleList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg delSysRuleById(int id) {
        logger.info("SysRuleServiceImpl delSysRuleById id{}",id);
        SysRule sysRule = new SysRule();
        sysRule.setId(id);
        sysRule.setIsDel(1);
        sysRule.setModifyDate(new Date());
        int count = this.baseMapper.updateById(sysRule);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }

    @Override
    public ResultMsg updSysRuleById(SysRule sysRule) {
        logger.info("SysRuleServiceImpl updSysRuleById sysRule{}",sysRule);
        int count = this.baseMapper.updateById(sysRule);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }
}
